package com.itheima.day18.xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ParseXml {
    public static void main(String[] args) throws Exception {
        SAXReader sax = new SAXReader();
        Document document = sax.read(new File("day 18XML&DTD&schma&,枚举类型\\xml\\stu.xml"));
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements("student");
        ArrayList<Student> list = new ArrayList<>();
        for (Element element : elements) {
            Attribute id = element.attribute("id");
            String idValue = id.getValue();
            Element name = element.element("name");
            String nameText = name.getText();
            Element age = element.element("age");
            String ageText = age.getText();
            Student stu = new Student(idValue, nameText, Integer.parseInt(ageText));
            list.add(stu);
        }
        for (Student student : list) {
            System.out.println(student);
        }

    }
}
